﻿using System;
using Resources;
using Wicresoft.RDW.Presentation;
using Wicresoft.RDW.Utility;

namespace Wicresoft.RDW.UserControls
{
    public partial class RentRequestControl : System.Web.UI.UserControl
    {
        public string Language
        {
            get { return BasePage.Language.ToString(); }
        }
        protected BasePage BasePage
        {
            get
            {
                return this.Page as BasePage;
            }
        }
        public Model.RentRequest RentRequestData
        {
            get
            {
                int personNumber;
                bool result = int.TryParse(txtRentNumber.Text, out personNumber);
                Model.RentRequest data = new Model.RentRequest()
                {
                    RequestId = BasePage.Id,
                    Purpose = Server.HtmlEncode(txtRentPurpose.Text),
                    RentPerson = txtRentName.Text.Trim(),
                    PersonNum = personNumber,
                    OtherRequirements = Server.HtmlEncode(txtOtherRequirments.Text)
                };
                if (!string.IsNullOrEmpty(hfRentRequestId.Value))
                {
                    data.Id = Convert.ToInt32(hfRentRequestId.Value);
                }
                else
                {
                    data.Id = 0;
                }
                data.RequestId = BasePage.Id;
                return data;
            }
        }
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                if (BasePage.PageStatus == PageStatus.Readonly)
                {
                    Wicresoft.RDW.Utility.Helper.ControlEditable(this, false);
                }
                else
                {
                    Wicresoft.RDW.Utility.Helper.ControlEditable(this, true);
                }
                InitResource();
                InitControl();
                LoadData();
            }
        }
        protected void InitResource()
        {
            rfvRentPurpose.ErrorMessage = NameResource.Rent_Purpose + CommonResource.NotNull;
            rfvRentName.ErrorMessage = NameResource.Rent_Name + CommonResource.NotNull;
            rfvRentNumber.ErrorMessage = NameResource.Rent_Number + CommonResource.NotNull;
            rvRentNumber.ErrorMessage = NameResource.Rent_Number + CommonResource.ErrorFormat;
        }

        protected void InitControl()
        {
            //Load Current User
            Model.User user = new Model.User();
            if (BasePage.Id != 0)
            {
                var request = RequestPresent.GetById(BasePage.Id);
                user = UserPresent.GetById(request.RequestUserId);   
            }
            else
            {
                user = UserPresent.GetById(BasePage.CurrentUserId);
            }
            if (user != null)
            {
                txtUserName.Text = BasePage.Language.ToString() == "CN" ? user.NameCN : user.NameEN;
                txtDep.Text = user.BD;
                txtCellphone.Text = user.Mobile;
                txtTelephone.Text = user.OfficePhone;
                txtEmail.Text = user.Email;
                txtUserName.ReadOnly = true;
                txtDep.ReadOnly = true;
                txtCellphone.ReadOnly = true;
                txtTelephone.ReadOnly = true;
                txtEmail.ReadOnly = true;
            }
        }
        protected void LoadData()
        {
            if (BasePage.PageStatus == PageStatus.New)
                return;
            Model.RentRequest rentRequest = RentRequestPresent.GetByCondition(p => p.RequestId == BasePage.Id);
            if (rentRequest != null)
            {
                hfRentRequestId.Value = rentRequest.Id.ToString();

                txtOtherRequirments.Text = Server.HtmlDecode(rentRequest.OtherRequirements);
                txtRentPurpose.Text = Server.HtmlDecode(rentRequest.Purpose);
                txtRentName.Text = rentRequest.RentPerson;
                txtRentNumber.Text = rentRequest.PersonNum == 0 ? "" : rentRequest.PersonNum.ToString();
            }
        }
    }
}